package com.lagou.controller;

import com.lagou.entity.Student;
import com.lagou.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.lagou.Constants.*;

@RestController
public class StudentController {

    private Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    @PostMapping("/api/student/save")
    public Map<String, Object> save(Student student) {
        logger.info("save request student:{}", student);

        Map<String, Object> result = new HashMap<>();
        try {
            studentService.save(student);

            result.put(CODE, SUCCESS_CODE);
            result.put(MSG, "保存成功~");
        } catch (Exception e) {
            e.printStackTrace();

            result.put(CODE, FAIL_CODE);
            result.put(MSG, e.getMessage());
        }

        logger.info("save response result:{}", result);

        return result;
    }

    @PostMapping("/api/student/update")
    public Map<String, Object> update(Student student) {
        logger.info("update request student:{}", student);

        Map<String, Object> result = new HashMap<>();
        try {
            studentService.update(student);

            result.put(CODE, SUCCESS_CODE);
            result.put(MSG, "更新成功~");
        } catch (Exception e) {
            e.printStackTrace();

            result.put(CODE, FAIL_CODE);
            result.put(MSG, e.getMessage());
        }

        logger.info("update response result:{}", result);

        return result;
    }

    @PostMapping("/api/student/delete")
    public Map<String, Object> delete(int id) {
        logger.info("delete request id:{}", id);

        Map<String, Object> result = new HashMap<>();
        try {
            studentService.delete(id);

            result.put(CODE, SUCCESS_CODE);
            result.put(MSG, "删除成功~");
        } catch (Exception e) {
            e.printStackTrace();

            result.put(CODE, FAIL_CODE);
            result.put(MSG, e.getMessage());
        }

        logger.info("delete response result:{}", result);

        return result;
    }

    @GetMapping("/api/student/select")
    public Map<String, Object> select(int id) {
        logger.info("select request id:{}", id);

        Map<String, Object> result = new HashMap<>();
        try {
            result.put(DATA, studentService.select(id));

            result.put(CODE, SUCCESS_CODE);
            result.put(MSG, "查询成功~");
        } catch (Exception e) {
            e.printStackTrace();

            result.put(CODE, FAIL_CODE);
            result.put(MSG, e.getMessage());
        }

        logger.info("select response result:{}", result);

        return result;
    }

    @GetMapping("/api/student/selectAll")
    public Map<String, Object> selectAll() {
        logger.info("selectAll request");

        Map<String, Object> result = new HashMap<>();
        try {
            result.put(DATA, studentService.selectAll());

            result.put(CODE, SUCCESS_CODE);
            result.put(MSG, "查询成功~");
        } catch (Exception e) {
            e.printStackTrace();

            result.put(CODE, FAIL_CODE);
            result.put(MSG, e.getMessage());
        }

        logger.info("selectAll response result:{}", result);

        return result;
    }

}
